package SocialMediaSentiment;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import jxl.write.WriteException;

public class NaiveBayesZeroNonZero {

	public static void main(String[] args) throws IOException, WriteException {
		int i = 1;
		while (i <= 10) {
			i++;
			HashMap<String, String> toprintZero = new HashMap<String, String>();
			HashMap<String, String> toprintNonZero = new HashMap<String, String>();
			int totalZero = 0;
			int totalNonZero = 0;
			int totalZeroWrong = 0;
			int totalNonZeroWrong = 0;
			

			float[] sentimentChance = getSentimentChance();
			float sentimentChanceZero = sentimentChance[0];
			float sentimentChanceNonZero = sentimentChance[1];
			HashMap<String, Float> wordsCountAll = getWordsCount("mediumCorrectedCounterTrainSetAll.xls");
			HashMap<String, Float> wordsCountZero = getWordsCount("mediumCorrectedCounterTrainSetZero.xls");
			HashMap<String, Float> wordsCountNonZero = getWordsCount("mediumCorrectedCounterTrainSetNonZero.xls");
			float totalWordsZero = getTotalCount(wordsCountZero);
			float totalWordsNonZero = getTotalCount(wordsCountNonZero);
			System.out.println(totalWordsZero + "  " + totalWordsNonZero);
			HashMap<Integer, String[]> tweetsWithSentiment = ReadExcel.read(
					"mediumCorrectedTweets.xls", 0, 1);
			Iterator<Integer> k = (Iterator<Integer>) tweetsWithSentiment
					.keySet().iterator();
			while (k.hasNext()) {
				int index = k.next();
				String[] data = tweetsWithSentiment.get(index);
				String tweet = data[0];
				int sentiment = Integer.parseInt(data[1].replace(" ", ""));
				float posZero = getTweetPos(tweet, wordsCountZero, totalWordsZero,
						sentimentChanceZero, wordsCountAll);
				float posNonZero = getTweetPos(tweet, wordsCountNonZero, totalWordsNonZero,
						sentimentChanceNonZero, wordsCountAll);
				//System.out.println(posZero + " " + posNonZero);
				int classi = getClassification(posZero, posNonZero);
				if (sentiment == 0) {
					if (classi != 0) {
						toprintZero.put(tweet, "" + classi);
						totalZeroWrong++;
					}					
					totalZero++;
				} else {//sentiment != 0
					if (classi == 0)
					{
						
						totalNonZeroWrong++;
					}
					totalNonZero++;
					toprintNonZero.put(tweet, "" + classi);
				} 
				/*
				 * System.out.println(tweet); System.out.println(sentiment + "v"
				 * + classi + "--" + posPos + "||" + negPos + "||" + neutPos);
				 * 
				 * toprint.put(tweet + " " + sentiment, "" + posPos + "||" +
				 * negPos + "||" + neutPos);
				 */
			}
			writeExcel.write("wrongBayesClassZero.xls", toprintZero);
			writeExcel.write("wrongBayesClassnonZero.xls", toprintNonZero);
			System.out
					.println("||||||||||||||||||TRAINSET||||||||||||||||||||||||");
			System.out.println("\t" + "NoSentiment\t" + "Sentiment");
			System.out.println("total:\t" + totalZero + "\t\t" + totalNonZero);
			System.out.println("wrong:\t" + totalZeroWrong + "\t\t"
					+ totalNonZeroWrong);
			System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||");
			
			
			/////////////////////////TESTSETRUN/////////////////////////
			HashMap<String, String> toprintZeroTest = new HashMap<String, String>();
			HashMap<String, String> toprintNonZeroTest = new HashMap<String, String>();
			int totalZeroTest = 0;
			int totalNonZeroTest = 0;
			int totalZeroWrongTest = 0;
			int totalNonZeroWrongTest = 0;
			

			HashMap<Integer, String[]> tweetsWithSentimentTest = ReadExcel.read(
					"CorrectedTweetsValidation.xls", 0, 1);
			Iterator<Integer> kTest = (Iterator<Integer>) tweetsWithSentimentTest
					.keySet().iterator();
			while (kTest.hasNext()) {
				int index = kTest.next();
				String[] data = tweetsWithSentimentTest.get(index);
				String tweet = data[0];
				int sentiment = Integer.parseInt(data[1].replace(" ", ""));
				float posZero = getTweetPos(tweet, wordsCountZero, totalWordsZero,
						sentimentChanceZero, wordsCountAll);
				float posNonZero = getTweetPos(tweet, wordsCountNonZero, totalWordsNonZero,
						sentimentChanceNonZero, wordsCountAll);
				//System.out.println(posZero + " " + posNonZero);
				int classi = getClassification(posZero, posNonZero);
				if (sentiment == 0) {
					if (classi != 0) {
						toprintZeroTest.put(tweet, "" + classi);
						totalZeroWrongTest++;
					}					
					totalZeroTest++;
				} else {//sentiment != 0
					if (classi == 0)
					{
						
						totalNonZeroWrongTest++;
					}
					totalNonZeroTest++;
					toprintNonZeroTest.put(tweet, "" + classi);
				} 
				
			}
			//writeExcel.write("wrongBayesClassZeroTest.xls", toprintZeroTest);
			//writeExcel.write("wrongBayesClassnonZeroTest.xls", toprintNonZeroTest);
			System.out
					.println("|||||||||||||||||||TESTSET||||||||||||||||||||||||");
			System.out.println("\tt" + "NoSentiment\t" + "Sentiment");
			System.out.println("total:\t" + totalZeroTest + "\t\t" + totalNonZeroTest);
			System.out.println("wrong:\t" + totalZeroWrongTest + "\t\t"
					+ totalNonZeroWrongTest);
			System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||");
			///////////////////////////////////////////////////////////
			updateCounters
					.updateCounterFile(
							"mediumCorrectedCounterTrainSetZero.xls",
							"wrongBayesClassZero.xls",
							createSignificantCounter
									.getSignificantWords("mediumCorrectedCounterTrainSetAll.xls"));
			updateCounters
					.updateCounterFile(
							"mediumCorrectedCounterTrainSetNonZero.xls",
							"wrongBayesClassnonZero.xls",
							createSignificantCounter
									.getSignificantWords("mediumCorrectedCounterTrainSetAll.xls"));
									
			// updateCounters.updateCounterFile("smallCorrectedCounterTrainSetSignificantZero.xls",
			// "wrongBayesClassZero.xls",
			// createSignificantCounter.getSignificantWords("smallCorrectedCounterTrainSetAllSignificant10plus.xls"));
		}
	}

	public static int getClassification(float Zero, float NonZero) {
		int classification = 0;
		if(NonZero > Zero)
			classification = 1;
		return classification;
	}

	public static float getTweetPos(String tweet,
			HashMap<String, Float> wordsCount, float totalWords,
			float sentimentChance, HashMap<String, Float> wordsCountAll) {
		StringTokenizer tokenizer = new StringTokenizer(tweet);
		Float chance;
		float tweetPos = sentimentChance;
		// float words = tokenizer.countTokens();
		// tweetPos = 0;
		while (tokenizer.hasMoreTokens()) {
			String word = tokenizer.nextToken();
			if (wordsCountAll.keySet().contains(word)) {
				try {
					chance = (2 * wordsCount.get(word)) / totalWords;
				} catch (NullPointerException e) {
					chance = 1 / totalWords;
				}

				//System.out.println(chance + "  " + tweetPos + "   " + word);
				tweetPos = tweetPos * chance * 1000;
			}
		}
		return tweetPos;
	}

	public static HashMap<String, Float> getWordsCount(String file)
			throws IOException {
		HashMap<String, Float> wordsCount = new HashMap<String, Float>();
		HashMap<Integer, String[]> IndexWordsCounter = ReadExcel.read(file, 0,
				1);
		Iterator<Integer> k = (Iterator<Integer>) IndexWordsCounter.keySet()
				.iterator();
		while (k.hasNext()) {
			int index = k.next();
			String[] WordsCounter = IndexWordsCounter.get(index);
			String word = WordsCounter[0];
			float count = Float.parseFloat(WordsCounter[1]);
			wordsCount.put(word, (count));
		}
		return wordsCount;
	}

	public static float getTotalCount(HashMap<String, Float> wordsCount)
			throws IOException {
		float total = 0;
		Iterator<String> k = (Iterator<String>) wordsCount.keySet().iterator();
		total += wordsCount.keySet().size();
		while (k.hasNext()) {
			String word = k.next();
			float count = wordsCount.get(word);
			total += count;
		}
		return total;
	}

	public static float[] getSentimentChance() throws IOException {
		float[] sentimentChance = new float[2];
		float Zero = 0;
		float nonZero = 0;
		float total = 0;

		HashMap<Integer, String[]> IndexTweets = ReadExcel.read(
				"mediumCorrectedTweets.xls", 0, 1);
		Iterator<Integer> k = (Iterator<Integer>) IndexTweets.keySet()
				.iterator();
		while (k.hasNext()) {
			int index = k.next();
			String[] TweetSentiment = IndexTweets.get(index);
			int sentiment = Integer
					.parseInt(TweetSentiment[1].replace(" ", ""));
			if (sentiment != 0)
				nonZero++;
			else
				Zero++;
			total++;
		}
		sentimentChance[0] = Zero / total;
		sentimentChance[1] = nonZero / total;
		return sentimentChance;

	}

}
